package com.tanxin.bill.modules;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tanxin.bill.R;
import com.tanxin.bill.common.base.BaseActivity;
import com.tanxin.bill.common.base.BaseEvent;
import com.tanxin.bill.common.data.BillData;
import com.tanxin.bill.common.data.BillData_;
import com.tanxin.bill.common.data.CustomerData;
import com.tanxin.bill.common.utils.PermissionUtils;
import com.tanxin.bill.modules.adapter.BillAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.objectbox.Box;
import io.objectbox.android.AndroidScheduler;
import io.objectbox.query.Query;
import io.objectbox.reactive.DataObserver;

public class CustomerDetailActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recycle)
    RecyclerView recycle;

    private Box<BillData> billBox;
    private Query<BillData> billQuery;

    private CustomerData data;
    private BillAdapter billAdapter;

    public static void startActivity(Context context, CustomerData data) {
        Intent intent = new Intent(context, CustomerDetailActivity.class);
        intent.putExtra("data", data);
        context.startActivity(intent);
    }

    @Override
    protected int bindLayout() {
        return R.layout.customer_detail_activity;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        data = extras.getParcelable("data");
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        toolbar.setTitle(data.getName());
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationIcon(R.mipmap.base_back_white);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressedSupport();
            }
        });

        billBox = MyApp.getApplication().getBoxStore().boxFor(BillData.class);
        billQuery = billBox.query().equal(BillData_.customerDataToOneId,data.getId()).build();
        billQuery.subscribe(subscriptions).on(AndroidScheduler.mainThread())
                .observer(new DataObserver<List<BillData>>() {
                    @Override
                    public void onData(@NonNull List<BillData> data) {
                        billAdapter.setNewData(data);
                    }
                });

        billAdapter = new BillAdapter(null);
        recycle.setLayoutManager(new LinearLayoutManager(getContext()));
        recycle.setAdapter(billAdapter);
        billAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
    }

    @Override
    protected void onEventComing(BaseEvent event) {

    }

    @OnClick({R.id.img_phone, R.id.img_sms})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_phone:
                PermissionUtils.callPhone(this,data.getPhone());
                break;
            case R.id.img_sms:
                PermissionUtils.sendSms(this,data.getPhone());
                break;
            default:
                break;

        }
    }
}
